package com.google.code.chaotic.client;

import static com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_DEFAULT;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.geocode.DirectionsPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public final class RouteConfirmDialog extends DialogBox {

	public static enum Result {
		CONFIRM, MODIFY, CANCEL
	}

	private Result result = null;

	//

	public RouteConfirmDialog( //
			MapWidget mapWidget, DirectionsPanel directionsPanel) {

		super(false, true);

		this.setText("Route Confirmation");
		this.setGlassEnabled(true);
		this.setAnimationEnabled(true);

		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setSpacing(10);
		vPanel.add(new Label(
				"The following has been extracted from the link you've provided."
						+ "\nPlease check if it is correct..."));
		HorizontalSplitPanel hSplit = new HorizontalSplitPanel();
		hSplit.setSize("880px", "440px");
		hSplit.setSplitPosition("40%");
		// directionsPanel.setSize("200px", "400px");
		mapWidget.setSize("100%", "100%");
		hSplit.setLeftWidget(directionsPanel);
		hSplit.setRightWidget(mapWidget);
		vPanel.add(hSplit);
		// vPanel.setCellHeight(hSplit, "100%");
		HorizontalPanel buttonsPanel = new HorizontalPanel();

		final Button confirmButton = new Button("Confirm");
		Label blank1 = new Label();
		final Button modifyButton = new Button("Modify");
		Label blank2 = new Label();
		final Button cancelButton = new Button("Cancel");

		ClickHandler handler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Object source = event.getSource();
				if (source == confirmButton) {
					result = Result.CONFIRM;

				} else if (source == modifyButton) {
					result = Result.MODIFY;

				} else {
					result = Result.CANCEL;
				}
				hide();
			}
		};
		confirmButton.addClickHandler(handler);
		modifyButton.addClickHandler(handler);
		cancelButton.addClickHandler(handler);

		buttonsPanel.add(confirmButton);
		buttonsPanel.add(blank1);
		buttonsPanel.add(modifyButton);
		buttonsPanel.add(blank2);
		buttonsPanel.add(cancelButton);

		buttonsPanel.setCellWidth(blank1, "10px");
		buttonsPanel.setCellWidth(blank2, "10px");

		vPanel.add(buttonsPanel);
		vPanel.setCellHorizontalAlignment(buttonsPanel, // 
				Util.getReverseAlignment(ALIGN_DEFAULT));
		this.setWidget(vPanel);
	}

	public Result getResult() {
		return result;
	}
}